import { createRouter, createWebHistory } from "vue-router";
import home from "@/views/home.vue";
import about from "@/views/about.vue";
import login from "@/views/login.vue";
import article from "@/views/article.vue";
import { loginCheck } from "../helper";

const route = [
  {
    path: "/",
    name: "home",
    component: home,
    beforeEnter: () => {
      console.log("home--路由独享守卫beforeEnter");
    },
  },
  {
    path: "/about",
    name: "about",
    component: about,
    meta: { auth: false },
    beforeEnter: () => {
      console.log("about--路由独享守卫beforeEnter");
    },
  },
  {
    path: "/login",
    name: "login",
    component: login,
    beforeEnter: () => {
      console.log("路由独享守卫beforeEnter");
    },
  },
  {
    path: "/article",
    name: "article",
    component: article,
    beforeEnter: () => {
      console.log("路由独享守卫beforeEnter");
    },
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes:route,
});

router.beforeEach((to, from) => {
  // if (to.name == 'about') return from
  console.log("全局前置守卫beforeEach");
  // return false
  // return await new Promise(resolve => {
  //   resolve(false)
  // })
  // return new Promise((resolve, reject) => {
  //   setTimeout(() => resolve(true), 2000)
  // })
});

router.beforeResolve((to, from) => {
  console.log("全局解析守卫beforeResolve",'==>导航被确认');
});

const routes = [];
router.afterEach((to, from, fail) => {
  console.log("全局后置钩子afterEach",'==>触发 DOM 更新');

  if (!fail) {
    routes.push(to.path);
    localStorage.setItem("routes", JSON.stringify(routes));
  }
});
export default router;
